<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Think\Model;
use Api\Logic\UserLogic;

/**
 * 用户优惠券控制器
 * @author derek <693472824@qq.com>
 */
class UserCouponController extends BaseController {
    
    /**
     * 订单可用优惠券
     */
    public function orderCoupon(){
        
        $orderID = I('order_id');
        
        $userID = $this->checkLogin('UserCoupon/orderCoupon', array('order_id'=>$orderID));
        
        if(empty($orderID)){
            
            $this->error('参数错误');
        }else{
            
            $order = M('Orders')->where(array('order_id'=>$orderID, 'user_id'=>$userID))->find();
            
            if(!empty($order)){
            
                $couponModel = M('UserCoupon');
                
                $map['user_id'] = $userID;
                $map['status'] = '1';               //1：可用；2：已用；3：过期
                $map['deadline'] = array('ELT', NOW_TIME);
                
                //将已过期优惠券设置status = 3
                $couponModel->where($map)->setField('status', '3');
                
                $map['deadline'] = array('GT', NOW_TIME);
                $couponList = $couponModel->where($map)->order('deadline,minimum')
                                ->getField('coupon_id,code,category,minimum,money,create_time,deadline');
                
                $list = array();
                
                foreach ($couponList as $key=>$val){
                    
                    //定额优惠时订单总金额大于定额优惠金额 或 满减优惠时订单总金额大于优惠所需最少金额
                    if((empty($val['minimum']) && $order['total'] > $val['money']) 
                            || (!empty($val['minimum']) && $order['total'] >= $val['minimum'])){
                        
                        $list['available'][] = $val;
                    }else{          //满减优惠
                        $list['unavailable'][] = $val;
                    }
                }
                
                $this->assign('order_id', $orderID);
                $this->assign('list', $list);
            }
            
            $this->display();
        }
    }
    
    /**
     * 我的优惠券
     */
    public function index(){
        
        $userID = $this->checkLogin('UserCoupon/index');
        
        $map['status'] = '1';
        $map['deadline'] = array('ELT', NOW_TIME);
        
        //将已过期优惠券设置status = 3
        M('UserCoupon')->where($map)->setField('status', '3');
        
        $this->display();
    }
    
    /**
     * 获取优惠券
     */
    public function ajax_couponList(){
        
        $start = I('lastIndex');
        $start = empty($start) || !is_numeric($start) ? '0' : $start;
        
        $userID = UserLogic::is_login();
        
        if(empty($userID)){
        
            $this->result['status'] = '-201';
        }else{
        
            $type = I('tabIndex');
            $type = !in_array($type, array('1', '2')) ? '1' : $type;
            
            $map = array();
            $map['user_id'] = $userID;
            
            if($type == '1'){
                
                //1：可用；2：已用；3：过期
                $map['status'] = '1';                           
                $map['deadline'] = array('GT', NOW_TIME);
            }elseif($type == '2'){
                $map['status'] = array('in', '2,3');
            }
            
            $couponModel = M('UserCoupon');
            
            $total = $couponModel->where($map)->count();
            
            $list = $couponModel->where($map)->order('deadline,minimum')->limit($start, $this->limit)
                                ->getField('coupon_id,code,category,minimum,money,create_time,deadline');
            
            $this->result['status'] = '1';
            $list = array_values($list);
            $this->result['info'] = $list;
            
            if($start + $this->limit >= $total){
                $this->result['is_last'] = '1';
            }
        }
        
        $this->ajaxReturn($this->result);
    }
    
    /**
     * 兑换优惠券
     */
    public function exchange(){
        
        $userID = UserLogic::is_login();
        $code = I('code');
        
        if(empty($userID)){
            $this->result['status'] = '-201';
        }elseif(preg_match('/^[0-9]{7}$/', $code)){
            $this->result['status'] = '422';
        }else{
            
            $map['code'] = $code;

            $couponModel = M('UserCoupon');
            $coupon = $couponModel->where($map)->find();
            
            if(empty($coupon)){
                $this->result['status'] = '0';
            }elseif ($coupon['status'] != '1'){
                $this->result['status'] = $coupon['status'];
            }elseif ($coupon['user_id'] == $userID){
                $this->result['status'] = '-1';
            }else{
                $result = $couponModel->where($map)->setField('user_id', $userID);
                $this->result['status'] = '1';
            }
        }
        
        $this->ajaxReturn($this->result);
    }
    
}
